package com.eipc;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.eipc.info.R;
import com.eipc.util.DBHelper;
import com.eipc.util.HttpManager;
import com.eipc.util.Tools;
/**
 * 收件箱
 * @author x_liaolijun
 *
 * 2010-9-4 上午11:44:56
 *  
 * ReceiveActivity
 *
 */
public class ReceiveActivity extends RefreshCommonActivity {
	private int type;   //类型(由于重要通告和短信和我要报告一样的)
	private ListView mListView;
	private Cursor mCursor;
	private String mNoticeId;
	private String mReplyContent;
	private String mReplyTime;
	private String mUrl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent i = getIntent();
		if(i==null){
			type = ImportNoticeActivity.TYPE;
		}
		else{
			type = i.getIntExtra("type", ImportNoticeActivity.TYPE);
		}
		setContentView(R.layout.import_notice_receive);
		initView();
		updateView();
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				showDailog((Cursor)mListView.getItemAtPosition(position));
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mCursor = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE,DBHelper.TB_PERSON,DBHelper.FIELD_IMPORT_NOTICE_ISSUER,DBHelper.FIELD_PERSON_ID,new String[]{DBHelper.FIELD_IMPORT_NOTICE_APPTYPE},new String[]{String.valueOf(type)},"tb1."+DBHelper.FIELD_IMPORT_NOTICE_ISREAD+" asc,tb1."+DBHelper.FIELD_IMPORT_NOTICE_ID+" desc limit 100");
		mListView.setAdapter(new ContentAdapter(mCursor));
	}
	
	protected void initView(){
		super.initView();
		mListView = (ListView)findViewById(R.id.list1);
		
	}
	
	@Override
	protected void updateView() {
		super.updateView();
		mCursor = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE,DBHelper.TB_PERSON,DBHelper.FIELD_IMPORT_NOTICE_ISSUER,DBHelper.FIELD_PERSON_ID,new String[]{DBHelper.FIELD_IMPORT_NOTICE_APPTYPE},new String[]{String.valueOf(type)},"tb1."+DBHelper.FIELD_IMPORT_NOTICE_ISREAD+" asc,tb1."+DBHelper.FIELD_IMPORT_NOTICE_ID+" desc limit 100");
		mListView.setAdapter(new ContentAdapter(mCursor));
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mCursor!=null) mCursor.close();
	}
	
	/*
	 * 显示图片
	 */
	private void showPicture(boolean isOk,Bitmap bm){
		if(isOk){
			final Dialog dialog = new Dialog(this);
			dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
			View view = getLayoutInflater().inflate(R.layout.picture_dialog, null);
			ImageView iv = (ImageView)view.findViewById(R.id.iv_picture);
			iv.setImageBitmap(bm);
			dialog.setContentView(view);
			dialog.setCanceledOnTouchOutside(true);
			dialog.show();
		}
	}
	
	/*
	 * 创建对话框
	 * 
	 * @param info
	 */
	private void showDailog(final Cursor info) {
		if (info != null) {
			final Dialog dialog = new Dialog(this);
			dialog.setTitle(R.string.import_please_select);
			dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
			View view = getLayoutInflater().inflate(R.layout.control_center_dialog, null);
			TextView titleView = (TextView)view.findViewById(R.id.dialog_title);
			switch(type){
				case DBHelper.APP_TYPE_IMPORT_NOTICE://重要通知
					titleView.setText(getString(R.string.dialog_import_notice_title, getString(R.string.import_notice)));
					break;
				case DBHelper.APP_TYPE_ME_REPORT://我要找茬
					titleView.setText(getString(R.string.dialog_import_notice_title,getString(R.string.me_report)));
					break;
//				case DBHelper.APP_TYPE_WORK_REPORT://工作汇报
//					titleView.setText(getString(R.string.dialog_import_notice_title,getString(R.string.work_report)));
//					break;
			}
			TextView tv = (TextView) view.findViewById(R.id.issuer);
			tv.setText(Html.fromHtml(mRe.getString(R.string.control_signer,info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME)),info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_ISSUE_TIME)))));
			tv = (TextView) view.findViewById(R.id.sender);
			tv.setText(Html.fromHtml(mRe.getString(R.string.control_publishor,mDB.getPersonName(info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENDER))),info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SEND_TIME)))));
			Button b = (Button) view.findViewById(R.id.sure_close);
			mNoticeId = info.getString(1);
			Log.e("---------------mNoticeId----------------",mNoticeId);
			final EditText et = (EditText)view.findViewById(R.id.statuses);
			final Button down = (Button)view.findViewById(R.id.ib_down);
//			if(type==DBHelper.APP_TYPE_WORK_REPORT) et.setText(R.string.status_work_report);
			final int status = info.getInt(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_ISREAD));
			if(status==DBHelper.STATUS.IMPORT_NOTICE_UN_READ){
				tv = (TextView) view.findViewById(R.id.content);
				tv.setText(info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_CONTENT)));
				down.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						final ListView listView = (ListView)getLayoutInflater().inflate(R.layout.list, null);
						final Dialog dialog = new Dialog(ReceiveActivity.this);
						dialog.setTitle(R.string.import_please_select);
						ArrayAdapter<CharSequence> adapter = null;
//						if(type!=DBHelper.APP_TYPE_WORK_REPORT){
//							adapter = ArrayAdapter.createFromResource(
//				                ReceiveActivity.this, R.array.status, R.layout.spinner);
//						}
//						else{
							adapter = ArrayAdapter.createFromResource(
					                ReceiveActivity.this, R.array.status2, R.layout.spinner);
//						}
						listView.setOnItemClickListener(new OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
								et.setText((CharSequence)listView.getItemAtPosition(position));
								dialog.dismiss();
							}
						});
				        listView.setAdapter(adapter);
						dialog.setContentView(listView);
						dialog.show();
					}
				});
				b.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mReplyContent = (String)et.getText().toString();
						mReplyTime = Tools.getCurrentTimeString();
						dialog.dismiss();
						try{
							mDB.updateImportNotice(Integer.parseInt(mNoticeId),mReplyContent,mReplyTime);
							mDB.insertUploadReply(mNoticeId,mReplyContent,mReplyTime);
						}catch(Exception e){}
						try{
							Intent i;
							switch(type){
							case DBHelper.APP_TYPE_IMPORT_NOTICE://重要通知
								i= new Intent(ReceiveActivity.this,ImportNoticeActivity.class);
								break;
							case DBHelper.APP_TYPE_ME_REPORT://我要找茬
								i= new Intent(ReceiveActivity.this,MeReportActivity.class);
								break;
							default://工作汇报
								i= new Intent(ReceiveActivity.this,ImportNoticeActivity.class);
								break;
							}
							startActivity(i);
							finishMyself();
						}catch(Exception e){}
					}
				});
			}else{
				tv = (TextView) view.findViewById(R.id.content);
				tv.setText(info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_CONTENT))+"\n\n------------------------------------------\n"+getString(R.string.import_notice_replyed,info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_REPLYCONTENT)))+"\n回复时间 : "+info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_REPLYTIME)));
				RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.rlayout);
				rl.setVisibility(View.GONE);
				down.setVisibility(View.GONE);
				b.setText(R.string.btn_close);
				b.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						
						dialog.dismiss();
					}
				});
			}
			String webImage = info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_IMAGE_WEBURL));
			b = (Button)view.findViewById(R.id.btn_look);
			if(webImage!=null && !"".equals(webImage)){
				b.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						mUrl = info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_IMAGE_WEBURL));
						new LoadImageTask().execute();
					}
				});
			}
			else{
				b.setVisibility(View.GONE);
			}
			dialog.setContentView(view);
			dialog.show();
		}
	}
	
	public void finishMyself(){
		finish();
	}
	
	class ContentAdapter extends CursorAdapter{
		public ContentAdapter(Cursor cursor){
			super(ReceiveActivity.this, cursor);
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = getLayoutInflater().inflate(R.layout.control_center_item, null);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ImageView iv = (ImageView)view.findViewById(R.id.iv_type);
			String type = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SEND_TYPE));
			String isRead = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_ISREAD));
			//			Log.e("Isread", isRead);
			ImageView unreadImageView =(ImageView)view.findViewById(R.id.unread);
			if("0".equals(isRead)){
				//未读
				view.setBackgroundColor(Color.GRAY);
				unreadImageView.setVisibility(View.VISIBLE);
			}else{
				view.setBackgroundColor(Color.BLACK);
				unreadImageView.setVisibility(View.INVISIBLE);
				
			}
			
			int iType = Tools.getIconByType(type);
			iv.setImageResource(iType);
			TextView tv = (TextView)view.findViewById(R.id.content);
			//cursor.getString(0)+"-"+cursor.getString(1)+
			tv.setText("["+type+"]"+cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_CONTENT)));
			tv = (TextView) view.findViewById(R.id.signer);
			tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME)));
			tv = (TextView) view.findViewById(R.id.date);
			tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_ISSUE_TIME)));
		}	
	}
	
	class LoadImageTask extends CommonTask<Void>{
		private Bitmap mBitmap;

		public LoadImageTask(){
			super(ReceiveActivity.this,getString(R.string.loadImage));
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			Log.e("-------------------url : ",mUrl);
			if(mUrl!=null&&!"".equals(mUrl)){
				String u = mRe.getString(R.string.url_img,mUrl);
				Log.e("-------------------u : ",u);
				mBitmap = HttpManager.getURLBitmap(u);
			}else{
				showPicture(false,null);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Log.e("-------------------bm : ",String.valueOf(mBitmap));
			if(mBitmap != null){
				showPicture(true,mBitmap);
			}else{
				showPicture(false,null);
			}
		}
	}
}
